package edu.cuit.avatar.array;

import java.util.Arrays;
import java.util.Random;

/**
 * 数组的排序
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/3 15:29
 */
public class ArraySort {
    public static void selection(int[] list){
        //交换元素(最小的元素和当前迭代元素进行交换)
        for(int i = 0; i < list.length; i++){
            int min = list[i];
            int minIndex = i;

            //找出子序列中最小元素
            for(int j = i + 1; j < list.length; j++){
                if(min > list[j]){
                    min = list[j];
                    minIndex = j;
                }
            }

            //将当前元素和最小的元素进行交换
            if(minIndex != i){
                list[minIndex] = list[i];
                list[i] = min;
            }
        }
    }

    public static void main(String[] args) {
        int[] list = new int[20];
        Random random = new Random();
        for(int i = 0; i < list.length; i++)
            list[i] = random.nextInt(100);

        System.out.println(Arrays.toString(list));
        selection(list);
        System.out.println(Arrays.toString(list));
    }
}
